﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Epi;
using Epi.Core;
using Epi.Data;
using Epi.Menu;
using Menafrinet.ViewModel;
using Menafrinet.View.Controls;
using Microsoft.Office.Interop.Excel;
using System.Windows.Xps.Packaging; 

namespace Menafrinet.View.Controls
{
    /// <summary>
    /// Interaction logic for AnalysisPanel.xaml
    /// </summary>
    public partial class AnalysisPanel : UserControl
    {
        //const string PROJECT = "\\Projects\\";
        //const string PROJECTPATH = "Niger\\NigerCBS_V2.prj";
        private string district_str;
        private string start_year_str;
        private string end_year_str;
        private string week_str;
        private DateTime datePicker1_str;
        private DateTime datePicker2_str; 
        private System.Windows.Media.Brush BackGroundColor;

        private DataHelper DataHelper
        {
            get
            {
                return (this.DataContext as DataHelper);
            }
        }

        public AnalysisPanel()
        {
            InitializeComponent();
            //CanvasSelection.Margin = new Thickness(-150, 6, 0, 0);
        }
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            DistrictComboBox.SelectedIndex = 0;
            EpiStartYearComboBox.SelectedIndex = 0;
            EpiEndYearComboBox.SelectedIndex = 0;
            EpiWeekComboBox.SelectedIndex = 0;
            documentViewer1.Document = null;

            datePicker1.SelectedDate = null;
            datePicker2.SelectedDate = null;
        }
        private void Update_All_Click(object sender, RoutedEventArgs e)
        {
            SetSearchValues();
            string appDir = Environment.CurrentDirectory;
            Epi.Configuration.Load("Configuration\\EpiInfo.config.xml");

            //ProgressBar_Analysis.IsVisible(true);
            //ProgressBar_Analysis.Minimum = 0;
            //ProgressBar_Analysis.Maximum = 100;



            //Epi.Menu.EpiInfoMenuManager.CreatePermanentVariable("Permanent_Year_Beg", 1);
            //Epi.Menu.EpiInfoMenuManager.CreatePermanentVariable("Permanent_Year_End", 1);
            //Epi.Menu.EpiInfoMenuManager.CreatePermanentVariable("Permanent_District", 2);
            //Epi.Menu.EpiInfoMenuManager.SetPermanentVariable("Permanent_Year_Beg", start_year_str);
            //Epi.Menu.EpiInfoMenuManager.SetPermanentVariable("Permanent_Year_End", end_year_str);
            //Epi.Menu.EpiInfoMenuManager.SetPermanentVariable("Permanent_District", district_str);

            //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvasAndCreateOutput("Projects\\Menafrinet\\ClassificationByAgegroup.cvs7", "Projects\\Menafrinet\\Output\\Classification.html");

            //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvasAndCreateOutput("Projects\\Menafrinet\\Region-District.cvs7", "Projects\\Menafrinet\\Output\\Region-District.html");

            //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvasAndCreateOutput("Projects\\niger\\NmA.cvs7", "Projects\\niger\\Output\\NmA.html");

            //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvasAndCreateOutput("Projects\\niger\\Epicurve.cvs7", appDir + "\\Projects\\niger\\Output\\Epicurve.html");
            //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvasAndCreateOutput("Epicurve.cvs7", "Epicurve.html");

            //Epi.Menu.EpiInfoMenuManager.OpenAnalysisWithPGM("Projects\\Ghana\\LaunchEpicurve.pgm7");

            CanvasSelection.Margin = new Thickness(-170, 29, 0, 0);
                                //****================== Age Group Report ===========================****
            Microsoft.Office.Interop.Excel.Application app1 = new Microsoft.Office.Interop.Excel.Application();

            app1.Visible = true;
            var wb1 = app1.Workbooks.Open("C:\\TFSCode\\Menafrinet\\Menafrinet_NEW\\Menafrinet\\Menafrinet\\AgeGroupByConfirmation.xlsx");
            foreach (Worksheet ws1 in wb1.Worksheets)
            {
                ws1.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, "C:\\TFSCode\\Menafrinet\\Menafrinet_NEW\\Menafrinet\\Menafrinet\\AgeGroupByConfirmation.xps", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                wb1.Close(false, Type.Missing, Type.Missing);
                app1.Quit();
            }

            //****================== District Report ===========================****
            Microsoft.Office.Interop.Excel.Application app2 = new Microsoft.Office.Interop.Excel.Application();

            app2.Visible = true;
            var wb2 = app2.Workbooks.Open("C:\\TFSCode\\Menafrinet\\Menafrinet_NEW\\Menafrinet\\Menafrinet\\DistrictReport.xlsx");
            foreach (Worksheet ws2 in wb2.Worksheets)
            {
                ws2.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, "C:\\TFSCode\\Menafrinet\\Menafrinet_NEW\\Menafrinet\\Menafrinet\\DistrictReport.xps", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                wb2.Close(false, Type.Missing, Type.Missing);
                app2.Quit();
            }
        }
        private void tblockCategory_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender is TextBlock)
            {
                TextBlock txt = sender as TextBlock;
                txt.Foreground = this.Resources["categoryHover"] as SolidColorBrush;
                this.Cursor = Cursors.Hand;
            }
        }

        

        private void tblockCategory_MouseLeave(object sender, MouseEventArgs e)
        {
            if (sender is TextBlock)
            {
                TextBlock txt = sender as TextBlock;
                txt.Foreground = this.Resources["categoryNormal"] as SolidColorBrush;
                this.Cursor = Cursors.Arrow;
            }
        }

        private void tblockPlace_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //panelPlaces.Visibility = Visibility.Visible;
            //panelTime.Visibility = Visibility.Collapsed;
            //panelPeople.Visibility = Visibility.Collapsed;

            //panelHeaderPlaces.Visibility = Visibility.Visible;
            //panelHeaderTime.Visibility = Visibility.Collapsed;
            //panelHeaderPeople.Visibility = Visibility.Collapsed;
        }

        private void tblockTime_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //panelPlaces.Visibility = Visibility.Collapsed;
            //panelTime.Visibility = Visibility.Visible;
            //panelPeople.Visibility = Visibility.Collapsed;

            ////panelHeaderPlaces.Visibility = Visibility.Collapsed;
            //panelHeaderTime.Visibility = Visibility.Visible;
            //panelHeaderPeople.Visibility = Visibility.Collapsed;
        }

        private void tblockPerson_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //panelPlaces.Visibility = Visibility.Collapsed;
            //panelTime.Visibility = Visibility.Collapsed;
            //panelPeople.Visibility = Visibility.Visible;

            ////panelHeaderPlaces.Visibility = Visibility.Collapsed;
            //panelHeaderTime.Visibility = Visibility.Collapsed;
            //panelHeaderPeople.Visibility = Visibility.Visible;
        }

        private void BtnCasesByEpiweek_Click(object sender, RoutedEventArgs e)
        {
            SetSearchValues();
            string appDir = Environment.CurrentDirectory;
            Uri pageUri = new Uri(appDir + "\\projects\\Menafrinet\\Output\\Epicurve.html");
            //documentViewer1 = pageUri;
            //documentViewer1 = pageUri;

        }
        private void btnCases_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Xps.Packaging.XpsDocument d = new System.Windows.Xps.Packaging.XpsDocument("C:\\TFSCode\\Menafrinet\\Menafrinet_NEW\\Menafrinet\\Menafrinet\\DistrictReport.xps", System.IO.FileAccess.Read);
            documentViewer1.Document = d.GetFixedDocumentSequence(); 
        }

        private void BtnClassifyByAgeGroup_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Xps.Packaging.XpsDocument d1 = new System.Windows.Xps.Packaging.XpsDocument("C:\\TFSCode\\Menafrinet\\Menafrinet_NEW\\Menafrinet\\Menafrinet\\AgeGroupByConfirmation.xps", System.IO.FileAccess.Read);
            documentViewer1.Document = d1.GetFixedDocumentSequence(); 
        }

        private void Btn_NmA_Click(object sender, RoutedEventArgs e)
        {
            SetSearchValues();
            string appDir = Environment.CurrentDirectory;
            Uri pageUri = new Uri(appDir + "\\Projects\\Menafrinet\\Output\\NmA.html");
            //documentViewer1 = pageUri;
            //documentViewer1 = pageUri;
        }

        //private bool documentViewer1_DoubleClick(mshtml.IHTMLEventObj e)
        //{
        //    //MessageBox.Show("Switching to Full Screen");
        //    label1.Visibility = Visibility.Hidden;
        //    label2.Visibility = Visibility.Hidden;
        //    label3.Visibility = Visibility.Hidden;
        //    DistrictComboBox.Visibility = Visibility.Hidden;
        //    EpiStartYearComboBox.Visibility = Visibility.Hidden;
        //    EpiEndYearComboBox.Visibility = Visibility.Hidden;
        //    EpiWeekComboBox.Visibility = Visibility.Hidden;
        //    ClearButton.Visibility = Visibility.Hidden;
        //    documentViewer1.Visibility = Visibility.Hidden;
        //    ReportsText.Visibility = Visibility.Hidden;
        //    BtnCases.Visibility = Visibility.Hidden;
        //    BtnCasesByEpiweek.Visibility = Visibility.Hidden;
        //    BtnClassifyByAgeGroup.Visibility = Visibility.Hidden;
        //    NmALineList.Visibility = Visibility.Hidden;
        //    PerformanceIndicators.Visibility = Visibility.Hidden;
        //    SelectedEventButton.Visibility = Visibility.Hidden;

        //    documentViewer1.Visibility = Visibility.Visible;

        //    return true;
        //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvas(Environment.CurrentDirectory + "/Region-District.cvs7");
        ////Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvas(Environment.CurrentDirectory + "\\Projects\\Ghana\\Region-District.cvs7");
        //}

        //private bool documentViewer1_DoubleClick(mshtml.IHTMLEventObj e)
        //{
        //    //MessageBox.Show("Leaving Full Screen");
        //    label1.Visibility = Visibility.Visible;
        //    label2.Visibility = Visibility.Visible;
        //    label3.Visibility = Visibility.Visible;
        //    DistrictComboBox.Visibility = Visibility.Visible;
        //    EpiStartYearComboBox.Visibility = Visibility.Visible;
        //    EpiEndYearComboBox.Visibility = Visibility.Visible;
        //    EpiWeekComboBox.Visibility = Visibility.Visible;
        //    ClearButton.Visibility = Visibility.Visible;
        //    documentViewer1.Visibility = Visibility.Visible;
        //    ReportsText.Visibility = Visibility.Visible;
        //    BtnCases.Visibility = Visibility.Visible;
        //    BtnCasesByEpiweek.Visibility = Visibility.Visible;
        //    BtnClassifyByAgeGroup.Visibility = Visibility.Visible;
        //    NmALineList.Visibility = Visibility.Visible;
        //    PerformanceIndicators.Visibility = Visibility.Visible;
        //    SelectedEventButton.Visibility = Visibility.Visible;

        //    documentViewer1.Visibility = Visibility.Hidden;
        //    //documentViewer1.Focus();

        //    return true;


        //    //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvas(Environment.CurrentDirectory + "/Region-District.cvs7");
        //    //Epi.Menu.EpiInfoMenuManager.OpenDashboardWithCanvas(Environment.CurrentDirectory + "\\Projects\\Ghana\\Region-District.cvs7");
        //}    

        private void FlatColorButton_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void documentViewer1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            //mshtml.HTMLDocument doc = (mshtml.HTMLDocument)documentViewer1.Document;
            //mshtml.HTMLDocumentEvents2_Event iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
            //iEvent.ondblclick += new mshtml.HTMLDocumentEvents2_ondblclickEventHandler(documentViewer1_DoubleClick);
        }
        //private void documentViewer1_LoadCompleted(object sender, NavigationEventArgs e)
        //{
        //    mshtml.HTMLDocument doc = (mshtml.HTMLDocument)documentViewer1.Document;
        //    mshtml.HTMLDocumentEvents2_Event iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
        //    //iEvent.ondblclick += new mshtml.HTMLDocumentEvents2_ondblclickEventHandler(documentViewer1_DoubleClick);
        //}
        private void FlatColorButton_Loaded_1(object sender, RoutedEventArgs e)
        {

        }

        private void btnCases_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private void SetSearchValues()
        {
            district_str = (string)DistrictComboBox.SelectedValue;
            start_year_str = (string)EpiStartYearComboBox.SelectedValue;
            end_year_str = (string)EpiEndYearComboBox.SelectedValue;
            week_str = (string)EpiWeekComboBox.SelectedValue;
            //datePicker1_str = (DateTime)datePicker1.SelectedDate;
            //datePicker2_str = (DateTime)datePicker2.SelectedDate;

            if (string.IsNullOrEmpty(district_str))
                district_str = "AAA";
            if (string.IsNullOrEmpty(start_year_str))
                start_year_str = "1111";
            if (string.IsNullOrEmpty(end_year_str))
                end_year_str = "9999";
            if (string.IsNullOrEmpty(week_str))
                week_str = "     ";

        }

        private void PopulateDistrictComboBox()
        {
            DistrictComboBox.ItemsSource = null;
            DistrictComboBox.Items.Clear();

            if (System.IO.File.Exists(Environment.CurrentDirectory + "\\" + Properties.Settings.Default.ProjectPath))
            {
                Project project = new Project(Environment.CurrentDirectory + "\\" + Properties.Settings.Default.ProjectPath); //new Project(Environment.CurrentDirectory + PROJECT + PROJECTPATH);

                IDbDriver db = project.CollectedData.GetDatabase();
                System.Data.DataTable dt = db.GetTableData("codeDistricts1", "District", "District");

                DistrictComboBox.Items.Add(string.Empty);

                foreach (DataRow row in dt.Rows)
                {
                    DistrictComboBox.Items.Add(row[0].ToString());
                }

            }
        }// PopulateDistrictComboBox -- end
        //=========================================================================================
        private void PopulateEpiYearComboBoxs()
        {
            EpiStartYearComboBox.ItemsSource = null;
            EpiStartYearComboBox.Items.Clear();
            EpiEndYearComboBox.ItemsSource = null;
            EpiEndYearComboBox.Items.Clear();

            EpiStartYearComboBox.Items.Add(string.Empty);
            EpiEndYearComboBox.Items.Add(string.Empty);

            string[] years = { "2013", "2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", "2004", "2003", "2002", "2001", "2000", "1999", "1998", "1997" };
            for (int n_i = 0; n_i < years.Length; n_i++)
            {
                EpiStartYearComboBox.Items.Add(years[n_i]);
                EpiEndYearComboBox.Items.Add(years[n_i]);
            }

            //if (System.IO.Directory.Exists(Environment.CurrentDirectory + PROJECT))
            //{
            //    Project project = new Project(Environment.CurrentDirectory + PROJECT + PROJECTPATH);

            //    IDbDriver db = project.CollectedData.GetDatabase();
            //    DataTable dt = db.GetTableData("GhanaCBS1", "EpiYear", "EpiYear");

            //    foreach (DataRow row in dt.Rows)
            //    {
            //        if (!row[0].ToString().Equals(string.Empty))
            //        {
            //            if (IsDuplicateYear(EpiStartYearComboBox, row[0].ToString()) == false)
            //            {
            //                EpiStartYearComboBox.Items.Add(row[0].ToString());
            //                EpiEndYearComboBox.Items.Add(row[0].ToString());
            //            }
            //        }
            //    }
            //}
        }// PopulateEpiYearComboBoxs -- end
        //=========================================================================================
        private void PopulateEpiWeekComboBox()
        {
            EpiWeekComboBox.Items.Add(string.Empty);
            for (int n_i = 1; n_i <= 53; n_i++)
                EpiWeekComboBox.Items.Add(n_i.ToString());

        }// PopulateEpiWeekComboBox -- end
        //=========================================================================================
        private bool IsDuplicateYear(ComboBox EpiYearComboBox, string CurrentData)
        {
            int n_size = EpiYearComboBox.Items.Count;
            for (int n_i = 0; n_i < n_size; n_i++)
            {
                if (EpiYearComboBox.Items.GetItemAt(n_i).Equals(CurrentData))
                    return true;
            }
            return false;
        }// IsDuplicateYear -- end
        //=========================================================================================


        private void MouseEnteredBtnCases(object sender, MouseEventArgs e)
        {
            if (BtnCases.IsMouseOver == true)
            {
                EpiWeekComboBox.IsEnabled = false;
            }

        }// MouseEnteredBtnCases -- end
        //=========================================================================================
        private void MouseLeftBtnCases(object sender, MouseEventArgs e)
        {
            if (BtnCases.IsMouseOver == false)
            {
                EpiWeekComboBox.IsEnabled = true;
            }
        }
        private void LoadDropDownLists(object sender, RoutedEventArgs e)
        {
            PopulateDistrictComboBox();
            PopulateEpiYearComboBoxs();
            PopulateEpiWeekComboBox();
            BackGroundColor = BtnCases.Background;
        }

        private void BtnCasesByEpiweek_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void CanvasSelection_MouseLeave(object sender, MouseEventArgs e)
        {
            //Canvas c = (Canvas)sender;
            //DoubleAnimation Animation = new DoubleAnimation(0, TimeSpan.FromSeconds(.5));
            //c.BeginAnimation(Canvas.OpacityProperty, Animation);
            this.Width = 200;
        }

        private void CanvasSelection_MouseEnter(object sender, MouseEventArgs e)
        {
            //Canvas c = (Canvas)sender;
            //DoubleAnimation Animation = new DoubleAnimation(1, TimeSpan.FromSeconds(1));
            //c.BeginAnimation(Canvas.OpacityProperty, Animation);
            this.Width = 50;
        }


        private void ChangeCriteria_flatColorButton_MouseLeftButtonUp(object sender, RoutedEventArgs e)
        {
            if (CanvasSelection.Margin.Left == 0)
            {
                ChangeCriteria_flatColorButton.Content = "Modifier Les Critères";
                CanvasSelection.Margin = new Thickness(-170, 32, 0, 0);
                ChangeCriteria_flatColorButton.ToolTip = "Cliquez ici pour mettre à jour ou de modifier les rapports.";
                //DropShadowEffect Drop = null;
                //CanvasSelection.Effect = null;
                //Drop.Direction = 0;
                //Drop.BlurRadius = 0;
                //Drop.ShadowDepth = 0;
                //Drop.Color = Colors.Black;
                ////documentViewer1.Margin = new Thickness(246, 2, 7, 73);
                //documentViewer1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                ChangeCriteria_flatColorButton.Content = "Sous-Menu Collapse";
                CanvasSelection.Margin = new Thickness(0, 32, 0, 0);
                ChangeCriteria_flatColorButton.ToolTip = "Cliquez ici pour fermer ce menu";
                //DropShadowEffect Drop = new DropShadowEffect();
                //CanvasSelection.Effect = new DropShadowEffect();
                ////Drop.Direction = 305;
                //Drop.BlurRadius = 15;
                //Drop.ShadowDepth = 6;
                //Drop.Color = Colors.Black;
                //documentViewer1.Margin = new Thickness(396, 2, 7, 73);
                //documentViewer1.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}
